package com.yswy.backstage.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yswy.backstage.dto.req.*;
import com.yswy.backstage.dto.res.MenuTreeResVO;
import com.yswy.backstage.dto.res.PermissionConfigTreeResVO;
import com.yswy.backstage.dto.res.PermissionPageResVO;
import com.yswy.backstage.dto.res.RuleTreeResVO;
import com.yswy.domain.entity.user.Role;
import com.yswy.backstage.service.IAdminService;
import com.yswy.domain.vo.gld.filter.RolePageFilter;
import com.yswy.domain.vo.gld.filter.UserPageFilter;
import com.yswy.domain.vo.gld.addOrUpate.base.BaseReqVO;
import com.yswy.domain.vo.gld.filter.RolePageResVO;
import com.yswy.domain.vo.gld.filter.UserPageResVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

import static com.yswy.backstage.utils.GldTokenUtils.getUserId;


/**
 * 后台管理员
 *
 * @author linlifu
 * @date 2018/12/25
 */
@RestController
@RequestMapping("/admin")
@Api(tags = "管理端相关功能")
public class AdminController {
    private final Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private IAdminService adminService;

    @GetMapping("/menuTree")
    @ApiOperation(value = "权限菜单树")
    public List<MenuTreeResVO> menuTree(HttpServletRequest request) {
        return adminService.menuTree(getUserId(request), 1);
    }
    @GetMapping("/ruleTree")
    @ApiOperation(value = "权限 规则树")
    public List<RuleTreeResVO> ruleTree(@RequestParam(value = "menuId", required = false) Long menuId, HttpServletRequest request) {
        return adminService.ruleTree(menuId, getUserId(request));
    }
    @GetMapping("/configTree")
    @ApiOperation(value = "配置专用权限树")
    public List<PermissionConfigTreeResVO> permissionConfigTree(PermissionConfigTreeReqVO req, HttpServletRequest request) {
        return adminService.permissionConfigTree(req, getUserId(request));
    }


    @GetMapping("/permission/freezing")
    @ApiOperation(value = "权限 解冻|冻结")
    public void permissionFreezing(@RequestParam(value = "id") Long id,
                             @RequestParam(value = "freezingSatus") boolean freezingSatus, HttpServletRequest request) {
        adminService.permissionFreezing(id, freezingSatus, getUserId(request));
    }
    @PostMapping("/permission/page")
    @ApiOperation(value = "权限 分页")
    public Page<PermissionPageResVO> permissionPage(@RequestBody @Validated PermissionPageFilter filter, HttpServletRequest request) {
        return adminService.permissionPage(filter, getUserId(request));
    }
    @PostMapping("/permission/create")
    @ApiOperation(value = "权限 添加")
    public void permissionCreate(@RequestBody @Validated(BaseReqVO.CREATE.class) PermissionOptionReqVO req, HttpServletRequest request) {
        adminService.permissionCreate(req, getUserId(request));
    }
    @PostMapping("/permission/update")
    @ApiOperation(value = "权限 更新")
    public void permissionUpdate(@RequestBody @Validated(BaseReqVO.UPDATE.class) PermissionOptionReqVO req, HttpServletRequest request) {
        adminService.permissionUpdate(req, getUserId(request));
    }
    @GetMapping("/permission/delete")
    @ApiOperation(value = "权限 删除")
    public void permissionDelete(@RequestParam(value = "id") Long id, HttpServletRequest request) {
        adminService.permissionDelete(id, getUserId(request));
    }


    @PostMapping("/user/page")
    @ApiOperation(value = "用户 分页")
    public Page<UserPageResVO> userPage(@RequestBody @Validated UserPageFilter filter, HttpServletRequest request) {
        return adminService.userPage(filter, getUserId(request));
    }
    @GetMapping("/user/freezing")
    @ApiOperation(value = "用户 解冻|冻结")
    public void userFreezing(@RequestParam(value = "userId") Long userId,
                             @RequestParam(value = "freezingSatus") boolean freezingSatus, HttpServletRequest request) {
        adminService.userFreezing(userId, freezingSatus, getUserId(request));
    }
    @PostMapping("/user/create")
    @ApiOperation(value = "用户 添加")
    public void userCreate(@RequestBody @Validated(BaseReqVO.CREATE.class) UserOptionReqVO req, HttpServletRequest request) {
         adminService.userCreate(req, getUserId(request));
    }
    @PostMapping("/user/update")
    @ApiOperation(value = "用户 更新")
    public void userUpdate(@RequestBody @Validated(BaseReqVO.UPDATE.class) UserOptionReqVO req, HttpServletRequest request) {
         adminService.userUpdate(req, getUserId(request));
    }
    @GetMapping("/user/delete")
    @ApiOperation(value = "用户 删除")
    public void userDelete(@RequestParam(value = "id") Long id, HttpServletRequest request) {
         adminService.userDelete(id, getUserId(request));
    }

    @PostMapping("/role/page")
    @ApiOperation(value = "角色 分页")
    public Page<RolePageResVO> rolePage(@RequestBody @Validated RolePageFilter filter, HttpServletRequest request) {
        return adminService.rolePage(filter, getUserId(request));
    }
    @GetMapping("/role/list")
    @ApiOperation(value = "角色 列表")
    public List<Role> roleList(@RequestParam(value = "roleName", required = false) String roleName, HttpServletRequest request) {
        return adminService.roleList(roleName, getUserId(request));
    }
    @PostMapping("/role/create")
    @ApiOperation(value = "角色 添加")
    public void roleCreate(@RequestBody @Validated(BaseReqVO.CREATE.class) RoleOptionReqVO req, HttpServletRequest request) {
        adminService.roleCreate(req, getUserId(request));
    }
    @PostMapping("/role/update")
    @ApiOperation(value = "角色 更新")
    public void roleUpdate(@RequestBody @Validated(BaseReqVO.UPDATE.class) RoleOptionReqVO req, HttpServletRequest request) {
        adminService.roleUpdate(req, getUserId(request));
    }
    @GetMapping("/role/delete")
    @ApiOperation(value = "角色 删除")
    public void roleDelete(@RequestParam(value = "id") Long id, HttpServletRequest request) {
        adminService.roleDelete(id, getUserId(request));
    }
    @GetMapping("/role/freezing")
    @ApiOperation(value = "角色 解冻|冻结")
    public void roleFreezing(@RequestParam(value = "roleId") Long roleId,
                             @RequestParam(value = "freezingSatus") boolean freezingSatus, HttpServletRequest request) {
        adminService.roleFreezing(roleId, freezingSatus, getUserId(request));
    }

//    @PostMapping("/addPermission")
//    @ApiOperation(value = "添加权限")
//    public ResultModel addPermission(@RequestBody @Validated(PermissionPARAM.Create.class) PermissionPARAM permissionPARAM) {
//        return adminService.addPermission(permissionPARAM);
//    }
//
//    @PostMapping("/deletePermission")
//    @ApiOperation(value = "删除权限")
//    public ResultModel deletePermission(@RequestBody @Validated(PermissionPARAM.Delete.class) PermissionPARAM permissionPARAM) {
//        return adminService.deletePermission(permissionPARAM);
//    }
//
//    @PostMapping("/updatePermission")
//    @ApiOperation(value = "修改权限")
//    public ResultModel updatePermission(@RequestBody @Validated(PermissionPARAM.Update.class) PermissionPARAM permissionPARAM) {
//        return adminService.updatePermission(permissionPARAM);
//    }
//
//    @PostMapping("/listPermission")
//    @ApiOperation(value = "获取权限列表")
//    public ResultModel listPermission(@RequestBody PermissionPARAM permissionPARAM) {
//        return adminService.listPermission(permissionPARAM);
//    }
//
//    @PostMapping("/addRole")
//    @ApiOperation(value = "添加角色")
//    public ResultModel addRole(@RequestBody @Validated(RolePARAM.Create.class) RolePARAM rolePARAM) {
//        return adminService.addRole(rolePARAM);
//    }
//
//    @PostMapping("/deleteRole")
//    @ApiOperation(value = "删除角色")
//    public ResultModel deleteRole(@RequestBody @Validated(RolePARAM.Update.class) RolePARAM rolePARAM) {
//        return adminService.deleteRole(rolePARAM);
//    }
//
//    @PostMapping("/updateRole")
//    @ApiOperation(value = "修改角色")
//    public ResultModel updateRole(@RequestBody @Validated(RolePARAM.Create.class) RolePARAM rolePARAM) {
//        return adminService.updateRole(rolePARAM);
//    }
//
//    @GetMapping("/listRole")
//    @ApiOperation(value = "角色列表")
//    public ResultModel listRole(@RequestParam("userId") Integer userId) {
//        return adminService.listRole(userId);
//    }
//
//    @PostMapping("/appointRoleToUser")
//    @ApiOperation(value = "给用户分配角色")
//    public ResultModel appointRoleToUser(@RequestBody @Validated(UserPARAM.Appoint.class) UserPARAM userPARAM) {
//        return adminService.appointRoleToUser(userPARAM);
//    }
//
//    @PostMapping("/appointPermissionToRole")
//    @ApiOperation(value = "给角色分配权限")
//    public ResultModel appointPermissionToRole(@RequestBody @Validated(PermissionPARAM.Appoint.class) PermissionPARAM permissionPARAM) {
//        return adminService.appointPermissionToRole(permissionPARAM);
//    }
//
//    @PostMapping("/addUser")
//    @ApiOperation(value = "添加员工")
//    public ResultModel addUser(@RequestBody @Validated(UserPARAM.Create.class) UserPARAM userPARAM, HttpServletRequest request) {
//        User user = (User) request.getSession().getAttribute("user");
//        log.info("当前登录用户：" + user);
//        return adminService.addUser(user, userPARAM);
//    }
//
//    @PostMapping("/deleteUser")
//    @ApiOperation(value = "删除员工")
//    public ResultModel deleteUser(@RequestBody UserPARAM userPARAM, HttpServletRequest request) {
//        User user = (User) request.getSession().getAttribute("user");
//        return adminService.deleteUser(user, userPARAM);
//    }
//
//    @PostMapping("/updateUser")
//    @ApiOperation(value = "修改员工信息")
//    public ResultModel updateUser(@RequestBody @Validated(UserPARAM.Update.class) UserPARAM userPARAM, HttpServletRequest request) {
//        User user = (User) request.getSession().getAttribute("user");
//        return adminService.updateUser(user, userPARAM);
//    }
//
////    @PostMapping("/updateUserBalance")
////    @ApiOperation(value = "用户充值")
////    public ResultModel updateUserBalance(@RequestBody UserPARAM userPARAM, HttpServletRequest request) {
////        User user = (User) request.getSession().getAttribute("user");
////        return adminService.updateUserBalance(user, userPARAM);
////    }
//
//    @PostMapping("/listUser")
//    @ApiOperation(value = "获取员工列表")
//    public ResultModel listUser(@RequestBody @Validated(UserPARAM.GetList.class) UserPARAM userPARAM, HttpServletRequest request) {
//        User user = (User) request.getSession().getAttribute("user");
//        return adminService.listUser(user, userPARAM);
//    }
//
//    @GetMapping("/getCurrentUserInfo")
//    @ApiOperation(value = "获取当前用户信息")
//    public ResultModel getCurrentUserInfo(HttpServletRequest request) {
//        User user = (User) request.getSession().getAttribute("user");
//        return ResultModel.ok(user);
//    }
//
//    @GetMapping("/listMenu")
//    @ApiOperation(value = "菜单列表")
//    public ResultModel listMenu(HttpServletRequest request) {
//        User user = (User) request.getSession().getAttribute("user");
//        return adminService.listMenu(user);
//    }
}


